﻿using Microsoft.VisualBasic;
using pdfer.FileObjects;
using pdfer.Syntax;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Resources;
using System.Text;
using System.Threading.Tasks;

namespace pdfer.PdfObjecs
{
    internal class PagesDictionary: PdfFileObject
    {
        public PagesDictionary(PdfFileObject pdfFileObject)
            :base(pdfFileObject)
        {
            if (pdfFileObject.TryGetValue(PdfName.Key_Type, out object? val))
            {
                Type = PdfParserUtils.ParseString(val);
                if (Type != PdfTypeConst.Type_Pages)
                {
                    throw new Exception("类型必须是 /Pages");
                }
            }
        }
        /// <summary>
        /// /Type*   name    必须是/Pages
        /// </summary>
        public String Type { get; set; }

        private List<String> _kids;
        private Int32? _count;
        private String _parent;

        /// <summary>
        /// /Kids*   间接引用数组  此节点的直接子页面树节点
        /// </summary>
        public List<String> Kids
        {
            get
            {
                if (_kids == null && TryGetValue(PdfName.Key_Kids, out var obj))
                {
                    _kids = ParseList(obj, PdfParserUtils.Trans2String);
                }
                return _kids;
            }
        }
        /// <summary>
        /// /Count*  整数  页节点（不是其他页面树节点）的数量，它们是此节点的最终子节点
        /// </summary>
        public Int32 Count
        {
            get
            {
                if (_count == null && TryGetValue(PdfName.Key_Count, out var obj))
                {
                    _count = ParseInt32(obj);
                }
                return _count ?? 0;
            }
        }
        /// <summary>
        /// /Parent  间接引用页面树节点   引用此节点的父节点（此节点是其子节点）。如果不是页面树的根节点，则必须存在
        /// </summary>
        public String Parent
        {
            get
            {
                if (_parent == null && TryGetValue(PdfName.Key_Parent, out var obj))
                {
                    _parent = PdfParserUtils.ParseString(obj);
                }
                return _parent;
            }
        }

        #region 可继承的属性
        private Dictionary<String, Object>? _resources;
        private Int32 _rotate = -1;
        private List<Int32> _mediaBox;
        private List<Int32> _cropBox;
        /// <summary>
        /// 共享的资源集合 字体 图像等
        /// []空字典表示没有共享资源也不会从父级继承
        /// null 则会找到父级中的共享资源
        /// </summary>
        public Dictionary<String, Object>? Resources
        {
            get
            {
                if (_resources == null && TryGetValue(PdfName.Key_Resources, out var obj))
                {
                    _resources = ParseDictionary(obj);
                }
                if (_resources == null)
                {
                    // Todo从父级获取
                }
                return _resources;
            }
        }
        /// <summary>
        /// 可以继承
        /// </summary>
        public Int32 Rotate
        {
            get
            {
                if (_rotate == -1 && TryGetValue(PdfName.Key_Rotate, out var obj))
                {
                    _rotate = ParseInt32(obj);
                }
                if (_rotate == -1)
                {
                    // Todo从父级获取
                }
                return _rotate;
            }
        }
        /// <summary>
        /// 可以继承
        /// </summary>
        public List<Int32> MediaBox
        {
            get
            {
                if (_mediaBox == null && TryGetValue(PdfName.Key_MediaBox, out var obj))
                {
                    _mediaBox = ParseList(obj, PdfParserUtils.Trans2Int32);
                }
                if(_mediaBox == null)
                {
                    // Todo从父级获取
                }
                return _mediaBox;
            }
        }
        /// <summary>
        /// 可以继承
        /// </summary>
        public List<Int32> CropBox
        {
            get
            {
                if (_cropBox == null && TryGetValue(PdfName.Key_CropBox, out var obj))
                {
                    _cropBox = ParseList(obj, PdfParserUtils.Trans2Int32);
                }
                if (_cropBox == null)
                {
                    // Todo从父级获取
                }
                return _cropBox;
            }
        }
        #endregion
    }

}
